<?php
/**
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
class Bootstrap extends Yaf_Bootstrap_Abstract
{

    private $_config;

    public function _initConfig()
    {
        //把配置保存起来
        $this->_config = Yaf_Application::app()->getConfig();
        Yaf_Registry::set('config', $this->_config);

        //引入扩展函数
        Yaf_Loader::import(APPLICATION_PATH.'/library/Helper.php');
        Yaf_Loader::import(APPLICATION_PATH.'/library/MySQL/ez_sql_core.php');
        Yaf_Loader::import(APPLICATION_PATH.'/library/MySQL/ez_sql_mysqli.php');
    }

    public function _initPlugin(Yaf_Dispatcher $dispatcher)
    {
    }

    public function _initRoute(Yaf_Dispatcher $dispatcher)
    {
        $router = $dispatcher->getRouter();
        $route = new Yaf_Route_Simple("m", "c", "a");
        $router->addRoute("name", $route);
    }

    public function _initView(Yaf_Dispatcher $dispatcher)
    {
    }

    public function _initDb(Yaf_Dispatcher $dispatcher)
    {
        // init db
        $db_config = $this->_config->mysql->read->toArray();
		$this->_db = new ezSQL_mysqli($db_config['username'], $db_config['password'], $db_config['database_name'], $db_config['server']);

        Yaf_Registry::set('_db', $this->_db);
    }

    public function _initMemcached(Yaf_Dispatcher $dispatcher)
    {
    }

    public function _initSession($dispatcher)
    {
        Yaf_Session::getInstance()->start();
    }

    public function _initSmarty(Yaf_Dispatcher $dispatcher) {
        $smarty = new Smarty_Adapter(null, Yaf_Application::app()->getConfig()->smarty);
        Yaf_Dispatcher::getInstance()->setView($smarty);
    }
}
